<template>
	<view class="container">
		<view class="add-button" @click="add">
			<u-icon name="plus" color="#2979ff" size="28" style="text-align: center;"></u-icon>
		</view>
		<u-popup :show="show" :round="10" mode="bottom" @close="close" @open="open">
			<view style="height: 500rpx;">
				<view>
					<u-button @click="reset">重置</u-button>
					<view>
						<u-picker v-model="value1" :show="show1" :columns="columns" @confirm="handleConfirm" @cancel="handleCancel"></u-picker>
						<view style="margin: 30rpx 0 20rpx 0;">
							<text>{{textBtn}}</text>
						</view>
					</view>
					<view @click="show1 = true" style="width: 500rpx;margin-bottom: 60rpx;">
						<u--input
					    placeholder="请选择时间"
					    border="surround"
					    v-model="timeform.inputvalue"
					  ></u--input>
					</view>
					<u-button type="primary" :plain="true"  @click="insert">确定</u-button>
				</view>
			</view>
		</u-popup>
		<view style="float: left;margin-top: 40rpx">
			<view v-for="(item, index) in trainingTimeList" :key="index" class="box">
			  <text style="color: #2eafff;display: none;">{{ item.id }}</text>
			  <text style="color: #2eafff;">{{ item.trainingTime }}</text>
			  <!-- <view style="display: flex; justify-content: space-between; align-items: center;"></view> -->
			  <view style="width: 80rpx;margin-top: 20rpx;display: flex; align-items: center;margin-left: 57rpx;margin-top: 40rpx;">
				<view>
				  <u-tag plain icon="edit-pen" size="mini" @click="updateTime(item)"></u-tag>
				</view>
				<view style="width: 50rpx;background-color: #2eafff;margin-left: 50rpx;"></view>
				<view>
				  <u-tag plain type="error" icon="trash" size="mini" @click="removeTime(item)"></u-tag>
				</view>
			  </view>
			</view>
		</view>	
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//列表项
				item:{},
				//数组默认值
				trainingTimeList:[],
				//弹窗的显示隐藏，默认是false
				show:false,
				//选择日期的弹窗，默认是false
				show1:false,
				//选择日期绑定的值
				value1:'',
				//选择器列的默认值
				columns: [],
				//表单
				timeform:{
					id:0,
					//输入框默认值
					inputvalue:''
				},
				//类型
				operationType:'',
				//修改添加显示文字
				textBtn:'',
				//删除弹窗
				delshow:false
			}
		},
		onLoad(option) {
			//接收父组件传过来的值
			this.form=JSON.parse(option.item),
			//进入页面刷新列表
			this.getTrainingTimeList(this.form.id);
			//选择器自定义时间
			this.generateTimeOptions();
		},
		methods: {
			//选择器自定义时间
			generateTimeOptions() {
			   const startTime = 8; // 起始时间
			   const endTime = 20; // 结束时间
			   const options = [];
			   for (let i = startTime; i <= endTime; i++) {
					const startHour = i < 10 ? '0' + i : i;
					const endHour = i + 1 < 10 ? '0' + (i + 1) : i + 1;
					const timeStr = `${startHour}:00-${endHour}:00`;
					options.push({
					   text: timeStr,
					   value: timeStr,
					});
				}
			    this.columns = [options];
			},
			 // 点击自定义选择器时间的确定按钮
			handleConfirm(value) {
				this.value1 = value.value[0].value; // 存储选择的值
				this.timeform.inputvalue = value.value[0].text; // 将选择的日期值赋给inputvalue，以便显示
				this.show1 = false;
			},
			//添加弹窗
			add(){
				this.show=true;
				this.textBtn='添加';
				this.operationType='add';
				this.timeform.inputvalue='';
			},
			//添加方法
			insert(){
				 if (!this.timeform.inputvalue) {
					uni.showToast({
						title: '请选择时间',
						icon: 'none',
						duration: 2000
					});
					return; // 如果没有选择时间，直接返回，不执行后续的插入操作
				}
				this.timeform.coachId=this.form.id;
				this.timeform.trainingTime=this.value1;
				const that = this;
				if (this.operationType === 'add') {
					uni.request({
						url: this.URL+'trainingTime/saveTrainingTime',
						method: 'post',
						data:this.timeform,
						success: function(res) {
							if(res.data.code===200){
								console.log(that.form.id)
								that.getTrainingTimeList(that.form.id);
								that.show=false;
								uni.showToast({
									icon: 'success',
									title: res.data.data,
								});
							}else{
								uni.showToast({
									title: res.data.data,
									icon: 'error',
									duration: 2000
								});
							}
						},
						fail: function(error) {
							// 处理请求失败
							console.error('添加失败,请检查网络');
						}
					})
				}else if (this.operationType === 'update'){
					console.log(that.item),
					// 将item的ID值添加到时间表单的form中
					this.timeform.id = that.item.id;
					uni.request({
						url: this.URL+'trainingTime/updateTrainingTime',
						method: 'put',
						data: that.timeform,
						success: function(res) {
							if(res.data.code===200){
								that.getTrainingTimeList(that.form.id);
								that.show=false;
								uni.showToast({
									icon: 'success',
									title: res.data.data,
								});
								that.timeform.trainingTime='';
							}else{
								uni.showToast({
									title: res.data.data,
									icon: 'error',
									duration: 2000
								});
							}
						},
						fail: function(error) {
							console.error('修改失败');
						}
					})
				}
				
			},
			//修改弹窗
			updateTime(item){
				this.operationType = 'update'; // 设置操作类型为修改
				this.timeform.inputvalue = item.trainingTime;
				this.item = { ...item }; // 将获取到的 item 赋值给 this.item
				this.show = true;
				this.textBtn='修改'
			},
			//删除方法
			removeTime(item){
				var that=this;
				const id = item.id;
				const coachId = item.coachId;
				uni.request({
					url: `${this.URL}trainingTime/deleteTrainingTime/${id}/${coachId}`,
					method: 'delete',
					success: function(res) {
						if(res.data.code==200){
							that.getTrainingTimeList(that.form.id);
						}else{
							
						}
					},
					fail: function(error) {
						// 处理请求失败
						console.error('删除失败');
					}
				})
			},
			// 点击取消按钮
			handleCancel() {
			   // this.show1 = false;
			   this.show1 = false;
			},
			// 列表查询
			getTrainingTimeList(id) {
				uni.request({
				  url: this.URL + 'trainingTime/selectTrainingTime', // 根据实际情况修改请求的 URL
				  method: 'GET',
					data: {
						id: id // 将id作为参数传递给后端接口
					},
				  success: (res) => {
					if (res.data.code === 200) {
						this.trainingTimeList = res.data.data
					} else {

					}
				  },
				  fail: (error) => {
					console.error('查询失败,请检查网络')
				  }
				})
			},
			reset() {
			  this.timeform.inputvalue = ''; // 将输入框的内容清空
			  this.show1 = false; // 关闭日期选择器组件
			},
			open() {
			  // console.log('open');
			  this.delshow = true
			},
			close() {
			  this.show = false,
			  this.delshow = false
			  // console.log('close');
			}
			
		},
	}
</script>

<style>
.container{
	width: 100%;
}
.box{
	width: 310rpx;
	height: 160rpx;
	/* border:1px solid #b2f1ff; */
	background-color: aliceblue;
	margin-bottom: 40rpx;
	float: left;
	margin-left: 40rpx;
	text-align: center;	
	box-shadow: 2px 2px 5px #5ab4eb; /* 添加阴影 */
}

.add-button{
	width: 160rpx;
	height: 75rpx;
	/* border:1px solid #2eafff; */
	display: flex;
	justify-content: center;
	align-items: center;
	border-radius: 7%;
	color: white;
	font-size: 27rpx;
	margin: 0 auto;
	margin-top: 40rpx;
	box-shadow: 2px 2px 5px #84b8e6;
}
</style>
